package com.coder.algorithm.leetcode

import com.coder.algorithm.struct.ListNode

object k_test {

    @JvmStatic
    fun main(args: Array<String>) {
//        t0056()
//        t0048()
//        t0073()
//        t0498()
//        t0014()
//        t0005()
//        t0151()
//        t0028()
//        t0541()
//        t0561()
//        t0156()
//        t0027()
//        t0485()
//        t0209()
//        t0118()
//        t0119()
//        t0556()
//        t0153()
//        t0026()
//        t0283()
//        t0707()
//        t0141()
//        t0142()
//        t0160()
//        t0019()
//        t0206()
//        t0203()
//        t0328()
//        t0234()
//        t0021()
    }

    private fun t0234() {
        val head = ListNode.create(intArrayOf(1,2,2,1))
        equals(true, l_0234.isPalindrome(head))
    }

    private fun t0328() {
        val head = ListNode.create(intArrayOf(1,2,3,4,5))
        val result = l_0328.oddEvenList(head)?.toList()
        equals(listOf(1,3,5,2,4), result ?: emptyList<Int>())
    }

    private fun t0707() {
        var linkedList = l_0707()
        linkedList.addAtHead(1);
        linkedList.addAtTail(3);
        println(linkedList.toList())
        linkedList.addAtIndex(1, 2);           //链表变为1-> 2-> 3
        equals(listOf(1, 2, 3), linkedList.toList())

        var result = linkedList.get(1);                  //返回2
        equals(2, result)

        linkedList.deleteAtIndex(1);               //现在链表是1-> 3
        equals(listOf(1, 3), linkedList.toList())

        result = linkedList.get(1);                      //返回3
        equals(3, result)
    }

    private fun t0283() {
        var nums = intArrayOf(0, 1, 0, 3, 12)
        l_0283.moveZeroes(nums)
        arraysEquals(intArrayOf(1, 3, 12, 0, 0), nums)

        nums = intArrayOf(0)
        l_0283.moveZeroes(nums)
        arraysEquals(intArrayOf(0), nums)
    }

    private fun t0026() {
        equals(2, l_0026.removeDuplicates(intArrayOf(1, 1, 2)))
        equals(5, l_0026.removeDuplicates(intArrayOf(0, 0, 1, 1, 1, 2, 2, 3, 3, 4)))
    }

    private fun t0153() {
        equals(1, l_0153.findMin(intArrayOf(3, 4, 5, 1, 2)))
        equals(0, l_0153.findMin(intArrayOf(4, 5, 6, 7, 0, 1, 2)))
        equals(11, l_0153.findMin(intArrayOf(11, 13, 15, 17)))
    }

    private fun t0556() {
        val s = "Let's take LeetCode contest"
        val expected = "s'teL ekat edoCteeL tsetnoc"
        equals(expected, l_0556.reverseWords(s))
    }

    private fun t0119() {
        var rowIndex = 3
        var expected = listOf(1, 3, 3, 1)
        equals(expected, l_0119.getRow(rowIndex))

        rowIndex = 0
        expected = listOf(1)
        equals(expected, l_0119.getRow(rowIndex))

        rowIndex = 1
        expected = listOf(1, 1)
        equals(expected, l_0119.getRow(rowIndex))
    }

    private fun t0118() {
        var numRows = 5
        var expected = listOf(listOf(1), listOf(1, 1), listOf(1, 2, 1), listOf(1, 3, 3, 1), listOf(1, 4, 6, 4, 1))
        equals(expected, l_0118.generate(numRows))

        numRows = 1
        expected = listOf(listOf(1))
        equals(expected, l_0118.generate(numRows))
    }

    private fun t0209() {
        var nums = intArrayOf(2, 3, 1, 2, 4, 3)
        var target = 7
        var expected = 2
        equals(expected, l_0209.minSubArrayLen(target, nums))

        target = 4
        nums = intArrayOf(1, 4, 4)
        expected = 1
        equals(expected, l_0209.minSubArrayLen(target, nums))

        target = 11
        nums = intArrayOf(1, 1, 1, 1, 1, 1, 1, 1)
        expected = 0
        equals(expected, l_0209.minSubArrayLen(target, nums))
    }

    private fun t0485() {
        var nums = intArrayOf(1, 1, 0, 1, 1, 1)
        var expected = 3
        equals(expected, l_0485.findMaxConsecutiveOnes(nums))

        nums = intArrayOf(1, 0, 1, 1, 0, 1)
        expected = 2
        equals(expected, l_0485.findMaxConsecutiveOnes(nums))
    }

    private fun t0027() {
        var nums = intArrayOf(3, 2, 2, 3)
        var `val` = 3
        var expected = 2
        equals(expected, l_0027.removeElement(nums, `val`))

        nums = intArrayOf(0, 1, 2, 2, 3, 0, 4, 2)
        `val` = 2
        expected = 5
        equals(expected, l_0027.removeElement(nums, `val`))
    }

    private fun t0156() {
        var numbers = intArrayOf(2, 7, 11, 15)
        var target = 9
        var expected = intArrayOf(1, 2)
        arraysEquals(expected, l_0167.twoSum(numbers, target))

        numbers = intArrayOf(2, 3, 4)
        target = 6
        expected = intArrayOf(1, 3)
        arraysEquals(expected, l_0167.twoSum(numbers, target))

        numbers = intArrayOf(-1, 0)
        target = -1
        expected = intArrayOf(1, 2)
        arraysEquals(expected, l_0167.twoSum(numbers, target))
    }

    private fun t0561() {
        val nums = intArrayOf(6, 2, 6, 5, 1, 2)
        val expected = 9
        equals(expected, l_0561.arrayPairSum(nums))
    }

    private fun t0541() {
        val s = "abcdefg"
        val k = 2
        val expected1 = "bacdfeg"
        equals(expected1, l_0541.reverseStr(s, k))
    }

    private fun t0028() {
        val l = l_0028()
        var haystack = "hello"
        var needle = "ll"
        val output1 = 2
        equals(output1, l.strStr(haystack, needle))

        haystack = "aaaaa"
        needle = "bba"
        val output2 = -1
        equals(output2, l.strStr(haystack, needle))

        haystack = ""
        needle = ""
        val output3 = 0
        equals(output3, l.strStr(haystack, needle))
    }

    private fun t0151() {
        val l = l_0151()
        val input1 = "the sky is blue"
        val output1 = "blue is sky the"
        equals(output1, l.reverseWords(input1))

        val input2 = "  hello world  "
        val output2 = "world hello"
        equals(output2, l.reverseWords(input2))

        val input3 = "a good   example"
        val output3 = "example good a"
        equals(output3, l.reverseWords(input3))

        val input4 = " M6eAL   RxnYFfCA kibo     oEw    z36tp     kufsBMitF K  sMG A 1 H io"
        val output4 = "io H 1 A sMG K kufsBMitF z36tp oEw kibo RxnYFfCA M6eAL"
        equals(output4, l.reverseWords(input4))
    }

    private fun t0005() {
        val l = l_0005()
        val str1 = "babad"
        val res1 = "bab"
        equals(res1, l.longestPalindrome(str1))

        val str2 = "cbbd"
        val res2 = "bb"
        equals(res2, l.longestPalindrome(str2))
    }

    private fun t0014() {
        val l = l_0014()
        val strs1 = arrayOf("flower", "flow", "flight")
        val result1 = "fl"
        equals(result1, l.longestCommonPrefix(strs1))

        val strs2 = arrayOf("dog", "racecar", "car")
        val result2 = ""
        equals(result2, l.longestCommonPrefix(strs2))
    }

    private fun t0498() {
        val l = l_0498()
        val matrix1 = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9))
        val result1 = intArrayOf(1, 2, 4, 7, 5, 3, 6, 8, 9)
        arraysEquals(result1, l.findDiagonalOrder(matrix1))
    }

    private fun t0073() {
        val l = l_0073()
        val matrix1 = arrayOf(intArrayOf(1, 1, 1), intArrayOf(1, 0, 1), intArrayOf(1, 1, 1))
        val result1 = arrayOf(intArrayOf(1, 0, 1), intArrayOf(0, 0, 0), intArrayOf(1, 0, 1))
        l.setZeroes(matrix1)
        deepArraysEquals(result1, matrix1)

        val matrix2 = arrayOf(intArrayOf(0, 1, 2, 0), intArrayOf(3, 4, 5, 2), intArrayOf(1, 3, 1, 5))
        val result2 = arrayOf(intArrayOf(0, 0, 0, 0), intArrayOf(0, 4, 5, 0), intArrayOf(0, 3, 1, 0))
        l.setZeroes(matrix2)
        deepArraysEquals(result2, matrix2)
    }

    private fun t0048() {
        val l = l_0048()
        val matrix1 = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9))
        val result1 = arrayOf(intArrayOf(7, 4, 1), intArrayOf(8, 5, 2), intArrayOf(9, 6, 3))
        l.ratate_solution2(matrix1)
        deepArraysEquals(result1, matrix1)

        val matrix2 = arrayOf(
            intArrayOf(5, 1, 9, 11),
            intArrayOf(2, 4, 8, 10),
            intArrayOf(13, 3, 6, 7),
            intArrayOf(15, 14, 12, 16)
        )
        val result2 = arrayOf(
            intArrayOf(15, 13, 2, 5),
            intArrayOf(14, 3, 4, 1),
            intArrayOf(12, 6, 8, 9),
            intArrayOf(16, 7, 10, 11)
        )
        l.ratate_solution2(matrix2)
        deepArraysEquals(result2, matrix2)
    }

    private fun t0056() {
        val l = l_0056()

        val matrix1 = arrayOf(intArrayOf(1, 3), intArrayOf(2, 6), intArrayOf(8, 10), intArrayOf(15, 18))
        val result1 = arrayOf(intArrayOf(1, 6), intArrayOf(8, 10), intArrayOf(15, 18))
        deepArraysEquals(result1, l.merge(matrix1))

        val matrix2 = arrayOf(intArrayOf(1, 4), intArrayOf(4, 5))
        val result2 = arrayOf(intArrayOf(1, 5))
        deepArraysEquals(result2, l.merge(matrix2))
    }

    private fun deepArraysEquals(expected: Array<IntArray>, result: Array<IntArray>) {
        if (expected.contentDeepToString() == result.contentDeepToString()) {
            println("通过：" + expected.contentDeepToString())
        } else {
            println("失败")
            println("预期：" + expected.contentDeepToString())
            println("结果：" + result.contentDeepToString())
        }
    }

    private fun arraysEquals(expected: IntArray, result: IntArray) {
        if (expected.contentToString() == result.contentToString()) {
            println("通过：" + expected.contentToString())
        } else {
            println("失败")
            println("预期：" + expected.contentToString())
            println("结果：" + result.contentToString())
        }
    }

    private fun equals(expected: Any, result: Any) {
        if (expected == result) {
            println("通过：$expected")
        } else {
            println("失败")
            println("预期：$expected")
            println("结果：$result")
        }
    }
}